package com.mdinfo.gw.door.service.impl

import com.mdinfo.base.common.bean.CommonResult
import com.mdinfo.base.common.bean.DataGrid
import com.mdinfo.base.common.bean.Ref
import com.mdinfo.base.utils.SystemUtil
import com.mdinfo.base.utils.TimeUtil
import com.mdinfo.gw.door.bean.DoorAccess
import com.mdinfo.gw.door.mapper.DoorAccessMapper
import com.mdinfo.gw.door.mapper.DoorMapper
import com.mdinfo.gw.door.param.*
import com.mdinfo.gw.door.service.IDoorAccessService
import com.mdinfo.gw.door.vo.DoorAccessListByDeptVo
import com.mdinfo.gw.door.vo.DoorAccessListQueryVo
import com.mdinfo.system.bean.SysDept
import com.mdinfo.system.mapper.SysDeptMapper
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*

/**
 * Created by Administrator on 2018/1/9.
 */
@Service
@Transactional
class DoorAccessServiceImpl(
        val doorAccessMapper : DoorAccessMapper,
        val deptMapper: SysDeptMapper,
        val doorMapper: DoorMapper
) : IDoorAccessService{

    /**添加门禁*/
    override fun add(param: DoorAccessAddParam)
            : CommonResult<Void> {

        var doorAccess:DoorAccess? = doorAccessMapper.getByDevId(param.devId!!)

        if(doorAccess!=null){
            return Ref.error("门禁设备编号已存在")
        }
        doorAccess = DoorAccess()
        doorAccess.createTime = Date()
        doorAccess.deptId = param.deptId!!
        doorAccess.lng = param.lng!!
        doorAccess.lat = param.lat!!
        doorAccess.name = param.name!!
        doorAccess.devId = param.devId!!
        doorAccess.host = param.host!!
        doorAccess.port = param.port
        doorAccess.devBrand = param.devBrand!!
        doorAccess.devModel = param.devModel!!
        doorAccess.devVersion = param.devVersion!!

        doorAccessMapper.insert(doorAccess)
        return Ref.ok()
    }

    /**列表查询*/
    override fun listQuery(param: DoorAccessListQueryParam)
            : CommonResult<DataGrid<DoorAccessListQueryVo>>{

        val info = SystemUtil.getInfo()
        val list = doorAccessMapper.listQuery(param.start!!,
                param.num!!,
                param.key,
                param.deptId,
                info.deptIdList)
        val vos:List<DoorAccessListQueryVo>  = list.map {
            val vo : DoorAccessListQueryVo = DoorAccessListQueryVo()
            vo.createTime = TimeUtil.formater(it.createTime)
            vo.devId = it.devId
            vo.doorAccessId = it.id
            vo.lat = it.lat
            vo.lng = it.lng
            vo.name = it.name
            vo.deptId = it.deptId
            vo.onLine = it.onLine
            val dept:SysDept? = deptMapper.selectById(it.deptId)
            vo.deptName = dept?.shortName?:""

            vo.host = it.host
            vo.port = it.port
            vo.devBrand = it.devBrand
            vo.devModel = it.devModel
            vo.devVersion = it.devVersion

            vo
        }
        val count = doorAccessMapper.countListQuery(
                param.key,
                param.deptId,
                info.deptIdList)
        var dataGrid = DataGrid<DoorAccessListQueryVo>()
        dataGrid.total = count
        dataGrid.rows = vos
        return Ref.ok(dataGrid)
    }

    /**修改门禁*/
    override fun  modify(param: DoorAccessModifyParam)
            : CommonResult<Void>{
        var doorAccess:DoorAccess? = doorAccessMapper.selectById(param.doorAccessId)
        if(doorAccess==null){
            return Ref.error("错误的门禁编号")
        }
        doorAccess.devId = param.devId!!
        doorAccess.name = param.name!!
        doorAccess.deptId = param.deptId!!
        doorAccess.lat = param.lat!!
        doorAccess.lng = param.lng!!
        doorAccess.host = param.host!!
        doorAccess.port = param.port
        doorAccess.devBrand = param.devBrand!!
        doorAccess.devModel = param.devModel!!
        doorAccess.devVersion = param.devVersion!!
        doorAccessMapper.updateById(doorAccess)
        return Ref.ok()
    }


    /**删除门禁*/
    override fun toDel(param: DoorAccessToDelParam)
            : CommonResult<Void>{
        var doorAccess:DoorAccess? = doorAccessMapper.selectById(param.doorAccessId)
        if(doorAccess==null){
            return Ref.error("错误的门禁编号")
        }
        //判断是否有绑定了门
        val doors = doorMapper.listDoorByDoorAccess(param.doorAccessId!!)
        if(doors.size>0){
            return Ref.error("该门禁绑定了门,不能删除")
        }
        doorAccessMapper.deleteById(param.doorAccessId)
        return Ref.ok()
    }

    /**本部门下的门禁控制器*/
    override fun listByDept(param: DoorAccessListByDeptParam)
            : CommonResult<List<DoorAccessListByDeptVo>>{

        var list = doorAccessMapper.getBydept(param.deptId!!)
        var vos = list.map {
            var vo = DoorAccessListByDeptVo()
            vo.doorAccessId = it.id
            vo.name = it.name
            vo
        }
        return Ref.ok(vos)
    }

}